package com.wunu.smartlock1.util;

import android.widget.Button;

import com.clj.fastble.BleManager;
import com.clj.fastble.callback.BleScanCallback;
import com.clj.fastble.data.BleDevice;
import com.clj.fastble.scan.BleScanRuleConfig;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import kotlin.collections.AbstractMutableList;

/**
 * Created by admin on 2018/5/20.
 */

public class BlueUtil {


   public static final int ScanTimes = 4*1000;

    public static boolean scanState = false;
    public static List<BleDevice> devices = new ArrayList<BleDevice>();
    static {
        if (!BleManager.getInstance().isSupportBle()) {
            System.out.println("该设备不支持蓝牙");
        } else {
            BleManager.getInstance().enableBluetooth();
            BleScanRuleConfig configure = new BleScanRuleConfig.Builder()
                    .setScanTimeOut(ScanTimes)
                    .build();
            BleManager.getInstance().initScanRule(configure);
        }
    }
    public static void startScan() {
        scanState = false;
        devices.clear();
        System.out.println("util start scan ...");
        BleManager.getInstance().scan(new BleScanCallback() {
            @Override
            public void onScanFinished(List<BleDevice> scanResultList) {
                scanState = true;
                System.out.println("💣-------------------- scanned count = " + scanResultList.size());
            }

            @Override
            public void onScanStarted(boolean success) {
                System.out.println("💣 start scan ------------------------" + success + "💣");
            }

            @Override
            public void onScanning(BleDevice result) {
                System.out.println("正在扫描蓝牙..."+ result.getName());
                if (result == null) return;
                System.out.println("💣" + result.getName() + "\t" + result.getMac() + "💣");
                int index = -1;
                for (int i = 0; i < devices.size(); i++) {
                    if (devices.get(i).getMac().equals(result.getMac())) {
                        System.out.println("两个蓝牙mac一样!" + devices.get(i).getMac() + "\t" + result.getMac());
                        index = i;
                    }
                }
                System.out.println("" + index + "移除前:" + devices.size());
                if (index >= 0) {
                    devices.remove(index);
                }
                System.out.println("移除后2:" + devices.size()+"-->"+result.getName());
                //只显示以WSL_开头的蓝牙设备
                if(result.getName() !=null && result.getName().startsWith("WSL_")){
                    devices.add(result);
                }
            }
            @Override
            public void onLeScan(BleDevice bleDevice) {
              //  System.out.println("onLeScan ..."+bleDevice.getMac());
            }

        });
    }
}
